package com.ljh.SimpleTradingSystem.controller;

import com.ljh.SimpleTradingSystem.mybatis.entity.User;
import com.ljh.SimpleTradingSystem.mybatis.mapper.UserMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    HttpServletRequest req;

    @Resource
    UserMapper userMapper;

    @GetMapping("/id")
    @ResponseBody
    public String getUserId() {
        return String.valueOf(req.getSession().getAttribute("userId"));
    }

    @GetMapping("/name")
    @ResponseBody
    public String getUserName(@RequestParam(value = "id", required = false) String id) {
        if (id == null) id = getUserId();
        User user = userMapper.selectById(Integer.parseInt(id));
        return user.getName();
    }

    @GetMapping("/state")
    @ResponseBody
    public String getState() {
        String userId = String.valueOf(req.getSession().getAttribute("userId"));
        if (!userId.equals("null")) {
            return "已登录";
        } else {
            return "未登录";
        }
    }
}
